import numpy as np
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg


root = tk.Tk()
fig = plt.figure()
plt.plot(np.random.rand(10))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

# # method 1
# def quit():
#     plt.close()
#     root.destroy()
# root.protocol('WM_DELETE_WINDOW', quit) #退出前先关闭plt
#
# # method 2
# root.protocol('WM_DELETE_WINDOW', root.quit) #退出前杀死全部进程。
root.mainloop()
